package com.yixun.qfbao.controller.agency.weapp;

import com.yixun.common.page.Page;
import com.yixun.qfbao.api.service.*;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.common.response.CommonResponse;
import com.yixun.qfbao.model.Customer;
import com.yixun.qfbao.model.dto.CustomerDto;
import com.yixun.qfbao.request.BaseRequest;
import com.yixun.qfbao.request.GenericIdRequest;
import com.yixun.qfbao.request.PageRequest;
import com.yixun.qfbao.request.customer.SaveOrEditCustomerReqDto;
import com.yixun.qfbao.response.BaseResponse;
import com.yixun.qfbao.response.customer.CustomerConvert;
import com.yixun.qfbao.response.customer.CustomerHisReportResDto;
import com.yixun.qfbao.response.customer.CustomerPageResDto;
import com.yixun.qfbao.xf.model.CustomerPremises;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.CustomerPremisesService;
import com.yixun.qfbao.xf.service.PremisesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @description: 客户相关接口
 * @author: JHSong
 * @create: 2020-9-17
 **/
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/agency/weapp/customer")
@Api(tags = {"分销小程序-客户"})
public class CustomerController {

    @Autowired
    private CustomerApiService customerApiService;
    @Autowired
    private CustomerPremisesService customerPremisesService;
    @Autowired
    private PremisesService premisesService;

    @ApiOperation(notes = "获取报备列表分页", value="获取报备分页")
    @PostMapping("/getCustomerPage")
    public BaseResponse<Page<CustomerPageResDto>> getCustomerPage(@RequestBody PageRequest<GetCustomerPageReqDto> request){
        BaseResponse<Page<CustomerPageResDto>> res = new BaseResponse<>();
        String userId = request.getUserId();
        if(StringUtils.isBlank(userId)) {
            res.setCode(ResultEnum.USER_IS_NULL.getNo());
            res.setMsg(ResultEnum.USER_IS_NULL.getDes());
            return res;
        }
        request.getSearchDTO().setUserId(Long.valueOf(userId));
        Page<Customer> customerPage = customerApiService.getWeappCustomerPage(request.getSearchDTO(), request.getPageDTO());
        Page<CustomerPageResDto> resDtoPage = new Page<>();
        List<Customer> customerList = customerPage.getRows();

        // 获取客户被报备过的楼盘名称
        List<String> customerIds = customerList.stream()
                .map(item -> String.valueOf(item.getId())).collect(Collectors.toList());
        Map<String, String> intentionPremisesMap = customerApiService.getCustomerReportedPremises(customerIds, 4);

        List<CustomerPageResDto> resDtoList = customerList.stream()
                .map(item -> CustomerConvert.convertCustomer2PageResDto(item, intentionPremisesMap))
                .collect(Collectors.toList());
        resDtoPage.setRows(resDtoList);
        res.setData(resDtoPage);
        res.setCode(ResultEnum.SUCCESS.getNo());
        res.setMsg(ResultEnum.SUCCESS.getDes());
        return res;
    }

    @ApiOperation(notes = "新增/编辑客户", value="新增/编辑客户")
    @PostMapping("/addOrEditCustomer")
    public BaseResponse addOrEditCustomer(@RequestBody BaseRequest<SaveOrEditCustomerReqDto> request){
        BaseResponse response = new BaseResponse<>();
        SaveOrEditCustomerReqDto reqDto = request.getData();
        // 经纪人 和 主号不能缺失
        if(Objects.isNull(reqDto.getAgentId()) || StringUtils.isBlank(reqDto.getMainMobile())){
            response.setCode(ResultEnum.FAIL.getNo());
            response.setMsg(ResultEnum.CUSTOMER_PARAMS_LOST.getDes());
            return response;
        }

        Customer customer = new Customer();
        BeanUtils.copyProperties(request.getData(), customer);
        CommonResponse commonResponse = customerApiService.saveOrEditCustomer(customer);

        response.setCode(commonResponse.getResponseCode());
        response.setMsg(commonResponse.getResponseMessage());
        return response;
    }

    @ApiOperation(notes = "客户详情", value="客户详情")
    @PostMapping("/getCustomerDetail")
    public BaseResponse<CustomerDto> getCustomerDetail(@RequestBody BaseRequest<GenericIdRequest> request){
        BaseResponse<CustomerDto> response = new BaseResponse<>();
        Customer customer = customerApiService.getCustomerById(request.getData().getId());
        CustomerDto customerDto = CustomerConvert.convertCustomer2Dto(customer);
        response.setData(customerDto);
        response.setCode(ResultEnum.SUCCESS.getNo());
        response.setMsg(ResultEnum.SUCCESS.getDes());
        return response;
    }

    @ApiOperation(notes = "客户历史报备", value="客户历史报备")
    @PostMapping("/getCustomerHisReport")
    public BaseResponse<List<CustomerHisReportResDto>> getCustomerHisReport(@RequestBody BaseRequest<GenericIdRequest> request){
        BaseResponse<List<CustomerHisReportResDto>> response = new BaseResponse<>();
        List<CustomerPremises> cpList = customerApiService.getCustomerHisReport(request.getData().getId());
        List<CustomerHisReportResDto> resDtoList = cpList.stream()
                .map(CustomerConvert::convertCP2HisReport)
                .collect(Collectors.toList());
        response.setData(resDtoList);
        response.setCode(ResultEnum.SUCCESS.getNo());
        response.setMsg(ResultEnum.SUCCESS.getDes());
        return response;
    }

}
